<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">{{systemName}}</span>
      </div>
      <!-- <div class="desc">Ant Design 是西湖区最具影响力的 Web 设计规范</div> -->
    </div>
    <div class="login">
      <a-form @submit="onSubmit">
        <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" style="padding: 0 2px;margin-top: 10vh;">
          <a-tab-pane tab="账户密码登录" key="1">
            <a-alert type="error" :closable="true" v-if="error" :message="error" @close='onClose' showIcon
              style="margin-bottom: 24px;" />
            <a-form-item name="mobile">
              <a-input v-model="form.mobile" autocomplete="autocomplete" size="large" placeholder="请输入手机号">
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item name="password">
              <a-input v-model="form.password" size="large" placeholder="请输入密码" autocomplete="autocomplete"
                type="password">
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
          </a-tab-pane>
        </a-tabs>
        <div>
          <!-- <a-checkbox :checked="true" >自动登录</a-checkbox> -->
          <a style="float: right">忘记密码</a>
        </div>
        <a-form-item>
          <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit"
            type="primary">登录</a-button>
        </a-form-item>
        <div>
          <!-- 其他登录方式
          <a-icon class="icon" type="alipay-circle" />
          <a-icon class="icon" type="taobao-circle" />
          <a-icon class="icon" type="weibo-circle" />
          <router-link style="float: right" to="/dashboard/workplace" >注册账户</router-link> -->
        </div>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
  import CommonLayout from '@/layouts/CommonLayout'
  // import { getRoutesConfig } from '@/services/user'
  // import { setAuthorization } from '@/utils/request'
  // import {loadRoutes} from '@/utils/routerUtil'
  import { mapMutations } from 'vuex'
  import { message } from 'ant-design-vue';
  import { postLogin } from "@/services"
  export default {
    name: 'Login',
    components: { CommonLayout },
    data() {
      return {
        logging: false,
        error: '',
        // 表单
        form: {
          mobile: '13772842353',
          password: '123456',
          userId: 0,
        },
      }
    },
    computed: {
      systemName() {
        return this.$store.state.setting.systemName
      }
    },
    methods: {
      ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
      /**
       * 登录提交
       */
      async onSubmit(e) {
        e.preventDefault()
        let { form } = this
        // 验证
        if (form.mobile == '') {
          return message.warning('请输入手机号')
        }
        if (!/^1[23456789]\d{9}$/.test(form.mobile)) {
          return message.warning('手机号格式不正确')
        }
        if (form.password == '') {
          return message.warning('请输入密码')
        }
        this.logging = true
        // 请求
        let { code, msg, data } = await postLogin({
          mobile: this.form.mobile,
          password: this.form.password,
          userId: 0
        })
        this.logging = false
        if (code == 200) {
          this.setUser(data)
          message.success('登录成功');
          this.$router.push('/demo')
        } else {
          message.warning(msg);
        }
        return

      },
      onClose() {
        this.error = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .common-layout {
    .top {
      text-align: center;

      .header {
        height: 44px;
        line-height: 44px;

        a {
          text-decoration: none;
        }

        .logo {
          height: 44px;
          vertical-align: top;
          margin-right: 16px;
        }

        .title {
          font-size: 33px;
          color: @title-color;
          font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
          font-weight: 600;
          position: relative;
          top: 2px;
        }
      }

      .desc {
        font-size: 14px;
        color: @text-color-second;
        margin-top: 12px;
        margin-bottom: 40px;
      }
    }

    .login {
      width: 368px;
      margin: 0 auto;

      @media screen and (max-width: 576px) {
        width: 95%;
      }

      @media screen and (max-width: 320px) {
        .captcha-button {
          font-size: 14px;
        }
      }

      .icon {
        font-size: 24px;
        color: @text-color-second;
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color 0.3s;

        &:hover {
          color: @primary-color;
        }
      }
    }
  }
</style>